package cn.group01.git.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.group01.git.common.CodeMsg;
import cn.group01.git.common.ResponseResult;
import cn.group01.git.dto.OutportPageDTO;
import cn.group01.git.entity.Goods;
import cn.group01.git.entity.GoodsImport;
import cn.group01.git.entity.GoodsOutport;
import cn.group01.git.mapper.GoodsMapper;
import cn.group01.git.mapper.OutportMapper;
import cn.group01.git.service.GoodsOutportService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
/**
 * @description:
 * @author: 张庆昌
 * @date: 2023/7/13 11:14
 **/
@Service
public class OutportServiceImpl implements GoodsOutportService {
    @Autowired
    private OutportMapper outportMapper;
    @Autowired
    private GoodsMapper goodsMapper;


    public ResponseResult getList(OutportPageDTO outportPageDTO) {
        // 使用分页插件 完成数据查询
        PageHelper.startPage(outportPageDTO.getPage(), outportPageDTO.getLimit());

        // 获取分页数据
        List<GoodsOutport> list = outportMapper.getList(outportPageDTO);
        PageInfo<GoodsOutport> goodsOutportPageInfo = new PageInfo<GoodsOutport>(list);// 创建一个更多分页信息的对象

        // 总条数
        long total = goodsOutportPageInfo.getTotal();
        // 当前页数据
        List<GoodsOutport> goodsOutports = goodsOutportPageInfo.getList();
        System.out.println("goodsOutports ="+goodsOutports);
        return ResponseResult.success(CodeMsg.SUCCESS,goodsOutports,total);
    }


    public ResponseResult add(GoodsOutport goodsOutport) {
        // 数量为负数 给提示
        if (goodsOutport.getNumber() < 0){
            return ResponseResult.fail(CodeMsg.NUMBER_ERROR);
        }
        // 先查询该商品库存中的数量
        Integer quantity = goodsMapper.getQuantityByGoodsName(goodsOutport.getGoodsName());
        // 如果库存数量大于要出库的数量，则出库成功 否则提示库存不足
        if(quantity > goodsOutport.getNumber()){
            // 修改库存数量
            goodsMapper.updateQuantity(goodsOutport.getGoodsName(),goodsOutport.getNumber());
            outportMapper.add(goodsOutport);
            return ResponseResult.success(CodeMsg.OUTPORT_SUCCESS);
        }else
            return ResponseResult.fail(CodeMsg.NOT_ENOUGH_FAIL);
    }


    public GoodsOutport getById(Integer id) {
        GoodsOutport goodsOutport = outportMapper.getById(id);
        System.out.println("goodsOutport = " + goodsOutport);
        return goodsOutport;
    }


    public ResponseResult edit(GoodsOutport goodsOutport) {
        // 出库信息中出库数量 100
        Integer number = outportMapper.getNumberById(goodsOutport.getId());
        System.out.println("出库信息中出库数量 = " + number);
        // 查询商品对象
        Goods goods = goodsMapper.getByGoodsName(goodsOutport.getGoodsName());
        // 库存数量 50
        Integer quantity = goods.getQuantity();
        System.out.println("库存数量 = " + quantity);
        // 修改数量 110
        Integer newNumber = goodsOutport.getNumber();
        System.out.println("修改后数量 = " + newNumber);
        // 数量为负数 给提示
        if (newNumber < 0){
            return ResponseResult.fail(CodeMsg.NUMBER_ERROR);
        }
        if(newNumber-number > quantity){
            return ResponseResult.fail(CodeMsg.NOT_ENOUGH_FAIL);
        }else {
            // 传入库存表中
            Integer result = quantity-(newNumber-number);
            System.out.println("result = " + result);

            // 把需要修改的商品库存值和id传到edit方法中
            goodsMapper.edit(result,goods.getId());
            // 编辑入库表中的信息
            outportMapper.edit(goodsOutport);
            return ResponseResult.success(CodeMsg.SUCCESS);
        }

    }


    public ResponseResult batchDelete(Integer[] ids) {
       /* outportMapper.batchDelete(ids);
        return ResponseResult.success(CodeMsg.SUCCESS);*/
        for (Integer id : ids) {
            // 1
            GoodsOutport goodsOutport = outportMapper.getById(id);
            // 2
            Goods goods = goodsMapper.getByGoodsName(goodsOutport.getGoodsName());
            // 3
            Integer result = goods.getQuantity()+goodsOutport.getNumber();

            // 4
            goodsMapper.updateGoodsNumber(result,goods.getId());
        }
        // 5
        outportMapper.batchDelete(ids);
        return ResponseResult.success(CodeMsg.DELETE);
    }


    public void export(OutportPageDTO outportPageDTO, HttpServletResponse response) {
        // 根据班级查询学生信息
        List<GoodsOutport> goodsOutports = outportMapper.getList(outportPageDTO);
        //导出参数 参数1：表格标题 参数2：sheet名字 取为班级名 --> clazz
        ExportParams exportParams = new ExportParams("出库信息", outportPageDTO.getGoodsName());
        // 生成工作表
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, GoodsOutport.class, goodsOutports);

        try {
            // 设置响应格式 以附件的形式打开（不是直接打开）
            response.setContentType("application/force-download");// 设置强制下载不打开
            response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("出库信息表.xlsx","UTF-8"));// 设置文件名 指定编码格式
            // 获取输出流
            ServletOutputStream outputStream = response.getOutputStream();
            // 用流写出工作表
            workbook.write(outputStream);
            outputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
